package com.cat.dynamicPlanning02;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/rotated-digits/?envType=problem-list-v2&envId=dynamic-programming
 * @create 2025/11/4 18:35
 * @since JDK17
 */

public class Solution33 {
    static int[] h = {0, 1, 5, -1, -1, 2, 9, -1, 8, 6};
    int reverse(int n) {
        int ans = 0, t = n / 10, offset = 1;
        while (t > 0) {
            t /= 10;
            offset *= 10;
        }
        t = n;
        while (t > 0) {
            int x = n / offset % 10;
            if (h[x] == -1) {
                return 0;
            }
            ans += offset * h[x];
            offset /= 10;
            t /= 10;
        }
        return n != ans ? 1 : 0;
    }

    public int rotatedDigits(int n) {
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            ans += reverse(i);
        }

        return ans;
    }
}
